package org.robot.scm.utils;

import com.google.common.collect.Lists;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.robot.scm.pojos.BaseNotify;
import org.robot.scm.pojos.TargetNotify;
import org.robot.scm.pojos.config.BaseConfig;
import org.robot.scm.pojos.enums.HttpMethodEnum;
import org.robot.scm.pojos.enums.PlatformEnum;

/**
 * @Author : Awn create on 2024/4/22 15:57
 */
@Slf4j
public class SendNoticeHelper {

    /**
     * 发送通知
     */
    public static void sendNotice(List<TargetNotify> notices) {
        if (CollectionUtils.isEmpty(notices)) {
            return;
        }
        notices.forEach(e -> CompletableFuture.runAsync(() -> {
            if (Objects.equals(e.getHttpMethodEnum(), HttpMethodEnum.POST)) {
                RequestHelper.post(e.toUrl(), e.getContent());
                return;
            }
            RequestHelper.get(e.toUrl());
        }));
    }

    public static List<TargetNotify> build(BaseConfig config, BaseNotify notice, PlatformEnum platform) {
        List<TargetNotify> targetNotifies = Lists.newArrayList();

        // bark 通知
        targetNotifies.addAll(StreamUtils.convertAndDistinct(config.get().getBarks(platform), e -> notice.barkToNotifyBean(e, config)));

        //pushme通知
        targetNotifies.addAll(StreamUtils.convertAndDistinct(config.get().getPushme(platform), e -> notice.pushmeToNotifyBean(e, config)));

        // robot 通知
        targetNotifies.addAll(StreamUtils.convertAndDistinct(config.get().getRobots(platform), e -> notice.robotToNotifyBean(e, config)));

        // 对未设置url的进行过滤
        return StreamUtils.filter(targetNotifies, e -> Objects.nonNull(e) && StringUtils.isNotBlank(e.getNotifyUrl()));
    }

    /**
     * 非群发, sendTo 指定接收人
     */
    public static List<TargetNotify> build(BaseConfig config, BaseNotify notice, PlatformEnum platform, String sendTo) {
        List<TargetNotify> targetNotifies = Lists.newArrayList();

        // bark 通知
        List<String> barks = StreamUtils.filter(config.get().getBarks(platform), barkSn -> StringUtils.equals(barkSn, sendTo));
        targetNotifies.addAll(StreamUtils.convertAndDistinct(barks, e -> notice.barkToNotifyBean(e, config)));

        // robot 通知
        List<String> robots = StreamUtils.filter(config.get().getRobots(platform), fromId -> StringUtils.equals(fromId, sendTo));
        targetNotifies.addAll(StreamUtils.convertAndDistinct(robots, e -> notice.robotToNotifyBean(e, config)));

        //pushme通知
        List<String> pushmes = StreamUtils.filter(config.get().getPushme(platform), pushKey -> StringUtils.equals(pushKey, sendTo));
        targetNotifies.addAll(StreamUtils.convertAndDistinct(pushmes, e -> notice.pushmeToNotifyBean(e, config)));

        // 对未设置url的进行过滤
        return StreamUtils.filter(targetNotifies, e -> Objects.nonNull(e) && StringUtils.isNotBlank(e.getNotifyUrl()));
    }

    /**
     * 群发
     */
    public static void sendNotice(BaseConfig config, BaseNotify baseNotify, PlatformEnum platform) {
        sendNotice(build(config, baseNotify, platform));
    }

    /**
     * 非群发, sendTo 指定接收人
     */
    public static void sendNotice(BaseConfig config, BaseNotify baseNotify, PlatformEnum platform, String sendTo) {
        sendNotice(build(config, baseNotify, platform, sendTo));
    }
}
